package com.blog.controller;

import com.blog.entity.RestBean;
import com.blog.entity.comment.Comment;
import com.blog.service.CommentService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/comment")
public class CommentController {

    @Resource
    private CommentService service;

    @GetMapping("/count-uid")
    public RestBean<Integer> countUid(Integer uid) {
        return RestBean.success(service.getCommentCountByUid(uid));
    }

    @GetMapping("/aid")
    public RestBean<List<Comment>> aid(Integer aid) {
        return RestBean.success(service.getCommentsByAid(aid));
    }

    @PostMapping("/add")
    public RestBean<String> add(Comment comment) {
        String s = service.addComment(comment);
        return s == null ? RestBean.success("评论发布成功") : RestBean.failure(400, s);
    }

    @PostMapping("/delete")
    public RestBean<String> delete(Integer cid) {
        String s = service.deleteComment(cid);
        return s == null ? RestBean.success("评论删除成功") : RestBean.failure(400, s);
    }

}
